package PuzzlePieces;

import java.util.ArrayList;

public class SudokuBox {

	private int[][] commited = new int[3][3];
	private int[][] temporary = new int[3][3];
	private int overallRowStart = 0;
	private int overallRowEnd = 0;
	private int overallColStart = 0;
	private int overallColEnd = 0;
	private int boxID;
	private int zeroTemp = 9;
	private int zeroCommitted = 9;
	private ArrayList<Integer> committedUsedNumbers = new ArrayList<Integer>(9);
	private ArrayList<Integer> tempUsedNumbers = new ArrayList<Integer>(9);
	
	public void commit(){
		committedUsedNumbers.clear();
		for(int row = 0; row < 3; row++){
			for(int col = 0; col < 3; col++){
				commited[row][col] = temporary[row][col];
				if(temporary[row][col] != 0){
					committedUsedNumbers.add(new Integer(temporary[row][col]));
				}
			}
		}
		zeroCommitted = zeroTemp;
	}
	
	public void reset(){
		tempUsedNumbers.clear();
		for(int row = 0; row < 3; row++){
			for(int col = 0; col < 3; col++){
				temporary[row][col] = commited[row][col]; 
				if(commited[row][col] != 0){
					tempUsedNumbers.add(new Integer(commited[row][col]));
				}
			}
		}
		zeroTemp = zeroCommitted;
	}
	
	public SudokuBox(int colStart, int colEnd, int rowStart, int rowEnd){
		overallRowStart = rowStart;
		overallRowEnd = rowEnd;
		overallColStart = colStart;
		overallColEnd = colEnd;
		
		boxID = calculateBoxID(rowStart, colStart);
	}
	
	public int getLastRow(){
		return overallRowEnd;
	}
	
	public int getLastCol(){
		return overallColEnd;
	}
	
	public int getStartCol(){
		return overallColStart;
	}
	
	public int getStartRow(){
		return overallRowStart;
	}
	
	public int getMiddleRow(){
		return (overallRowStart+1);
	}
	
	public int getMiddleCol(){
		return (overallColStart+1);
	}
	
	public int getBoxID(){
		return boxID;
	}
	
	public void setNumber(int row, int col, int value){
		if(overallRowStart == row){
			if(overallColStart == col){
				temporary[0][0] = value;
				tempUsedNumbers.add(new Integer(value));
				zeroTemp--;
			}else if((overallColStart + 1) == col){
				temporary[0][1] = value;
				tempUsedNumbers.add(new Integer(value));
				zeroTemp--;
			}else if((overallColEnd) == col){
				temporary[0][2] = value;
				tempUsedNumbers.add(new Integer(value));
				zeroTemp--;
			}
		}else if((overallRowStart+1) == row){
			if(overallColStart == col){
				temporary[1][0] = value;
				tempUsedNumbers.add(new Integer(value));
				zeroTemp--;
			}else if((overallColStart + 1) == col){
				temporary[1][1] = value;
				tempUsedNumbers.add(new Integer(value));
				zeroTemp--;
			}else if((overallColEnd) == col){
				temporary[1][2] = value;
				tempUsedNumbers.add(new Integer(value));
				zeroTemp--;
			}
		}else if(overallRowEnd == row){
			if(overallColStart == col){
				temporary[2][0] = value;
				tempUsedNumbers.add(new Integer(value));
				zeroTemp--;
			}else if((overallColStart + 1) == col){
				temporary[2][1] = value;
				tempUsedNumbers.add(new Integer(value));
				zeroTemp--;
			}else if((overallColEnd) == col){
				temporary[2][2] = value;
				tempUsedNumbers.add(new Integer(value));
				zeroTemp--;
			}
		}
	}
	
	public boolean correct(){
		boolean result = false;
		int sum = 0;
		ArrayList<Integer> myNumbers = new ArrayList<Integer>();
		for(int row = 0; row < 3; row++){
			for(int col = 0; col < 3; col++){
				sum += temporary[row][col];
				if(myNumbers.contains(new Integer(temporary[row][col]))){
					return false;
				}
				myNumbers.add(new Integer(temporary[row][col]));
			}
		}
		if(sum == 45){
			result = true;
		}
		return result;
	}
	
	public ArrayList<Integer> getUsedNumbers(){
		return tempUsedNumbers;
	}
	
	public static int calculateBoxID(int overallRow, int overallCol){
		int ID = 0;
		if(overallRow >= 3 && overallRow < 6 ){
			if(overallCol >= 3 && overallCol < 6){
				ID = 4;
			}else if(overallCol >= 6){
				ID = 5;
			}else{
				ID = 3;
			}
		}else if(overallRow >= 6){
			if(overallCol >= 3 && overallCol < 6){
				ID = 7;
			}else if(overallCol >= 6){
				ID = 8;
			}else{
				ID = 6;
			}
		}else{
			if(overallCol >= 3 && overallCol < 6){
				ID = 1;
			}else if(overallCol >= 6){
				ID = 2;
			}else{
				ID = 0;
			}
		}
		return ID;
	}

}
